package org.stvd.core.web;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.stvd.core.util.DateUtil;

public class MailUtil {
    public static final String MAIL_DEBUG = "true";
    public static final String MAIL_SERVER_HOST = "smtp.163.com";
    public static final String MAIL_SEND_FROM = "sys_report@163.COM";
    public static final String MAIL_TRANSPORT_PROTOCOL = "smtp";
    public static final String MAIL_SMTP_AUTH = "true";
    // QQ的smtp端口使用587端口
    public static final String MAIL_SMTP_PORT = "587";
    
    public static final String USER = "sys_report";
    public static final String PASSWORD = "sysreport789456";

    public static void sendEmail(String subject, String mailContent, String[] receivers) {
        String content = mailContent;
        try {
            Properties prop = new Properties();
            prop.setProperty("mail.debug", MAIL_DEBUG);
            prop.setProperty("mail.host", MAIL_SERVER_HOST);
            prop.setProperty("mail.transport.protocol", MAIL_TRANSPORT_PROTOCOL);
            prop.setProperty("mail.smtp.auth", MAIL_SMTP_AUTH);
//            prop.setProperty("mail.smtp.port", "587"); 
            
            // 1、创建session
            Session session = Session.getInstance(prop);
            Transport ts = null;
            // 2、通过session得到transport对象
            ts = session.getTransport();
            // 3、连上邮件服务器
            ts.connect(USER, PASSWORD);
            // 4、创建邮件
            MimeMessage message = new MimeMessage(session);
            // 邮件发件人
            message.setFrom(new InternetAddress(MAIL_SEND_FROM)); 
            // 邮件的收件人
            InternetAddress[] address = new InternetAddress[receivers.length];  
            for (int i = 0; i < receivers.length; i++) {  
                address[i] = new InternetAddress(receivers[i]);  
            }  
            message.addRecipients(Message.RecipientType.TO, address);
            message.setSubject(subject); // 邮件的标题
            message.setSentDate(DateUtil.getSystemDate());
            
            
            // 邮件消息体
            content = content.replace("\r\n", "<br/>")
                    .replace("\n", "<br/>")
//                    .replace(" ", "&nbsp;")
                    .replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
            content = "<div style=\"width:800px;font-size:8px\">" + content + "</div>";
            MimeBodyPart mdp = createContent(content);
            MimeMultipart allPart = new MimeMultipart("mixed");  
            allPart.addBodyPart(mdp);  
            message.setContent(allPart);  
            message.saveChanges();
            /*
             * 简单发送方式
             * message.setText(content);
             */
            // 5、发送邮件
            ts.sendMessage(message, message.getAllRecipients());
            ts.close();
        } catch (Exception ex) { }
    }

    /**  
     * 根据传入的文件路径创建附件并返回  
     */ 
    public static MimeBodyPart createAttachment(String fileName) throws Exception {  
        MimeBodyPart attachmentPart = new MimeBodyPart();  
        FileDataSource fds = new FileDataSource(fileName);  
        attachmentPart.setDataHandler(new DataHandler(fds));  
        attachmentPart.setFileName(fds.getName());  
        return attachmentPart;  
    }  
 
    /**  
     * 根据传入的邮件正文body和文件路径创建图文并茂的正文部分  
     */ 
    public static MimeBodyPart createContent(String body)  
            throws Exception {  
        // 用于保存最终正文部分  
        MimeBodyPart contentBody = new MimeBodyPart();  
        // 用于组合文本和图片，"related"型的MimeMultipart对象  
        MimeMultipart contentMulti = new MimeMultipart("related");  
 
        // 正文的文本部分  
        MimeBodyPart textBody = new MimeBodyPart();  
        textBody.setContent(body, "text/html;charset=utf-8");  
        contentMulti.addBodyPart(textBody);  
 
        // 将上面"related"型的 MimeMultipart 对象作为邮件的正文  
        contentBody.setContent(contentMulti);  
        return contentBody;  
    } 
    
    /**  
     * 根据传入的邮件正文body和文件路径创建图文并茂的正文部分  
     */ 
    public static MimeBodyPart createContent(String body, String fileName)  
            throws Exception {  
        // 用于保存最终正文部分  
        MimeBodyPart contentBody = new MimeBodyPart();  
        // 用于组合文本和图片，"related"型的MimeMultipart对象  
        MimeMultipart contentMulti = new MimeMultipart("related");  
 
        // 正文的文本部分  
        MimeBodyPart textBody = new MimeBodyPart();  
        textBody.setContent(body, "text/html;charset=gbk");  
        contentMulti.addBodyPart(textBody);  
 
        // 正文的图片部分  
        MimeBodyPart jpgBody = new MimeBodyPart();  
        FileDataSource fds = new FileDataSource(fileName);  
        jpgBody.setDataHandler(new DataHandler(fds));  
        jpgBody.setContentID("logo_jpg");  
        contentMulti.addBodyPart(jpgBody);  
 
        // 将上面"related"型的 MimeMultipart 对象作为邮件的正文  
        contentBody.setContent(contentMulti);  
        return contentBody;  
    }
 
    /**  
     * 根据传入的 Seesion 对象创建混合型的 MIME消息  
     */ 
//    public static MimeMessage createMessage(Session session) throws Exception {  
//        String from = "test_hao@163.com;";  
//        String to = "test_hao@sina.cn";  
//        String subject = "创建内含附件、图文并茂的邮件！";  
//        String body = "<h4>内含附件、图文并茂的邮件测试！！！</h4> </br>" 
//                + "<a href = http://haolloyin.blog.51cto.com/> 蚂蚁</a></br>" 
//                + "<img src = \"cid:logo_jpg\"></a>";  
// 
//        MimeMessage msg = new MimeMessage(session);  
//        msg.setFrom(new InternetAddress(from));  
//        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));  
//        msg.setSubject(subject);  
// 
//        // 创建邮件的各个 MimeBodyPart 部分  
//        MimeBodyPart attachment01 = createAttachment("F:\\java\\Snake.java");  
//        MimeBodyPart attachment02 = createAttachment("F:\\java\\meng.mp3");  
//        MimeBodyPart content = createContent(body, "F:\\java\\logo.jpg");  
// 
//        // 将邮件中各个部分组合到一个"mixed"型的 MimeMultipart 对象  
//        MimeMultipart allPart = new MimeMultipart("mixed");  
//        allPart.addBodyPart(attachment01);  
//        allPart.addBodyPart(attachment02);  
//        allPart.addBodyPart(content);  
// 
//        // 将上面混合型的 MimeMultipart 对象作为邮件内容并保存  
//        msg.setContent(allPart);  
//        msg.saveChanges();  
//        return msg;  
//    }  
}